What would you do to make this code more "over-engineered"? [closed]
Posted
by Mez
on Stack Overflow
See other posts from Stack Overflow
or by Mez
Published on 2010-05-12T23:16:52Z
Indexed on
2010/05/12
23:24 UTC
Read the original article
Hit count: 270
A friend and I got bored, and, long story short, decided to make an over-engineered FizzBuzz in PHP
<?php
interface INumber
{
public function go();
public function setNumber($i);
}
class FBNumber implements INumber
{
private $value;
private $fizz;
private $buzz;
public function __construct($fizz = 3 , $buzz = 5)
{
$this->setFizz($fizz);
$this->setBuzz($buzz);
}
public function setNumber($i)
{
if(is_int($i))
{
$this->value = $i;
}
}
private function setFizz($i)
{
if(is_int($i))
{
$this->fizz = $i;
}
}
private function setBuzz($i)
{
if(is_int($i))
{
$this->buzz = $i;
}
}
private function isFizz()
{
return ($this->value % $this->fizz == 0);
}
private function isBuzz()
{
return ($this->value % $this->buzz == 0);
}
private function isNeither()
{
return (!$this->isBuzz() AND !$this->isFizz());
}
private function isFizzBuzz()
{
return ($this->isFizz() OR $this->isBuzz());
}
private function fizz()
{
if ($this->isFizz())
{
return "Fizz";
}
}
private function buzz()
{
if ($this->isBuzz())
{
return "Buzz";
}
}
private function number()
{
if ($this->isNeither())
{
return $this->value;
}
}
public function go()
{
return $this->fizz() . $this->buzz() . $this->number();
}
}
class FizzBuzz
{
private $limit;
private $number_class;
private $numbers = array();
function __construct(INumber $number_class, $limit = 100)
{
$this->number_class = $number_class;
$this->limit = $limit;
}
private function collectNumbers()
{
for ($i=1; $i <= $this->limit; $i++)
{
$n = clone($this->number_class);
$n->setNumber($i);
$this->numbers[$i] = $n->go();
unset($n);
}
}
private function printNumbers()
{
$return = '';
foreach($this->numbers as $number){
$return .= $number . "\n";
}
return $return;
}
public function go()
{
$this->collectNumbers();
return $this->printNumbers();
}
}
$fb = new FizzBuzz(new FBNumber());
echo $fb->go();
In theory, what could we/would you do to make it even more "over-engineered"?
© Stack Overflow or respective owner